//
//  CourseListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/23.
//

#import "CourseListCell.h"

@interface CourseListCell()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *precentLabel;
@property(nonatomic,strong)UIProgressView *progressView;
@property(nonatomic,strong)UILabel *countLabel;
@property(nonatomic,strong)UIButton *beginBtn;
@property(nonatomic, strong)UIView *swipeView;
@property(nonatomic, strong)UIButton *customButton;

@end


@implementation CourseListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.contentView.backgroundColor = KbgColor;
    
    self.currentAvailableWidth = [DeviceUtils currentAvailableWidth];
    
    
    // 添加侧滑按钮视图
    self.swipeView = [[UIView alloc]initWithFrame:CGRectMake(self.currentAvailableWidth-32,0,80,80)];
    self.swipeView.backgroundColor = UIColorHex(0xFE4A4B);
    self.swipeView.layer.cornerRadius = 10;
    self.swipeView.layer.masksToBounds = YES;
    self.customButton = [[UIButton alloc]initWithFrame:CGRectMake(30,0,50, 80)];
    [self.customButton setTitle:@"重置" forState:UIControlStateNormal];
    [self.customButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.customButton.titleLabel.font = JPUIFontPingFangSCRegular(13);
    [self.customButton addTarget:self action:@selector(resetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.swipeView addSubview:self.customButton];
    [self.contentView addSubview:self.swipeView];
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth-32, 80)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bgView];
   
   
    __weak typeof(self)self_weak_ = self;

    self.beginBtn = [[UIButton alloc]init];
    [self.beginBtn setTitle:@"开始背词" forState:UIControlStateNormal];
    [self.beginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.beginBtn.backgroundColor = KThemeColor;
    self.beginBtn.titleLabel.font = JPUIFontPingFangSCMedium(13);
    self.beginBtn.layer.cornerRadius = 14;
    self.beginBtn.layer.masksToBounds = YES;
    [self.beginBtn addTarget:self action:@selector(beginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.beginBtn];
    [self.beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@76);
        make.height.mas_equalTo(@28);
        make.right.mas_equalTo(@-12);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontPingFangSCMedium(17);
    self.titleLabel.textColor = KFontBlackColor;
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.top.mas_equalTo(@12);
        make.right.mas_equalTo(self_weak_.beginBtn.mas_left).offset(-12);
        make.height.mas_equalTo(@24);
    }];
    
    self.precentLabel = [[UILabel alloc]init];
    self.precentLabel.font = JPUIFontPingFangSCRegular(12);
    self.precentLabel.textColor = KFontGrayColor;
    [self.bgView addSubview:self.precentLabel];
    [self.precentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.top.mas_equalTo(self_weak_.titleLabel.mas_bottom).offset(6);
        make.height.mas_equalTo(@17);
    }];
    
    self.countLabel = [[UILabel alloc]init];
    self.countLabel.font = JPUIFontInterRegular(12);
    self.countLabel.textColor = KFontGrayColor;
    self.countLabel.textAlignment = NSTextAlignmentRight;
    [self.bgView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self_weak_.beginBtn.mas_left).offset(-16);
        make.centerY.mas_equalTo(self_weak_.precentLabel);
        make.height.mas_equalTo(@15);
    }];
    
    self.progressView = [[UIProgressView alloc]init];
    self.progressView.progressTintColor = KThemeColor;
    self.progressView.backgroundColor = [UIColor whiteColor];
    self.progressView.trackTintColor = KbgColor;
    self.progressView.progress = 0.5;
    [self.bgView addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.right.mas_equalTo(self_weak_.countLabel.mas_right);
        make.height.mas_equalTo(@4);
        make.top.mas_equalTo(self_weak_.precentLabel.mas_bottom).offset(4);
    }];
    
    
}

-(void)refreshWidth{
    self.bgView.frame = CGRectMake(self.bgView.origin.x, 0, self.currentAvailableWidth-32, 80);
    if(self.bgView.origin.x == 0){
        self.swipeView.frame = CGRectMake(self.currentAvailableWidth-32,0,80,80);
    }else{
        self.swipeView.frame = CGRectMake(self.currentAvailableWidth-32-80,0,80,80);
    }
}

#pragma mark - 重置点击
- (void)resetBtnClick{
    [self refreshWithIsSwipe:NO andDynamic:NO];
    if (self.delegate && [self.delegate respondsToSelector:@selector(resetTap:)]){
        [self.delegate resetTap:self];
    }
}

#pragma mark - 侧滑更新UI
-(void)refreshWithIsSwipe:(BOOL)isSwipe  andDynamic:(BOOL)isDynamic{
    __weak typeof(self)weakSelf = self;
    if(isSwipe == YES){
        if(isDynamic == YES){
            [UIView animateWithDuration:0.3 animations:^{
                weakSelf.bgView.frame = CGRectMake(-50, 0, self.currentAvailableWidth-32, 80);
                weakSelf.swipeView.frame = CGRectMake(self.currentAvailableWidth-32-80,0,80,80);
            } completion:^(BOOL finished) {
                
            }];
        }else{
            self.bgView.frame = CGRectMake(-50, 0, self.currentAvailableWidth-32, 80);
            self.swipeView.frame = CGRectMake(self.currentAvailableWidth-32-80,0,80,80);
        }
    }else{
        if(isDynamic == YES){
            [UIView animateWithDuration:0.3 animations:^{
                weakSelf.bgView.frame = CGRectMake(0, 0, self.currentAvailableWidth-32, 80);
                weakSelf.swipeView.frame = CGRectMake(self.currentAvailableWidth-32,0,80,80);
            } completion:^(BOOL finished) {
                
            }];
        }else{
            self.bgView.frame = CGRectMake(0, 0, self.currentAvailableWidth-32, 80);
            self.swipeView.frame = CGRectMake(self.currentAvailableWidth-32,0,80,80);
        }
    }
    
}

#pragma mark - 开始背词
-(void)beginBtnClick{
    if (self.delegate && [self.delegate respondsToSelector:@selector(beginTap:)]){
        [self.delegate beginTap:self];
    }
}

-(void)setModel:(CourseListModel *)model{
    _model = model;
    if([model.percent isEqualToString:@"100"]){
        //学完
        self.beginBtn.backgroundColor = UIColorHex(0xCCCCCC);
        self.beginBtn.enabled = NO;
    }else{
        self.beginBtn.backgroundColor = KThemeColor;
        self.beginBtn.enabled = YES;
    }
    self.titleLabel.text = model.courseName;
    self.precentLabel.text = [NSString stringWithFormat:@"已学习：%@%%",model.percent];
    self.countLabel.text = [NSString stringWithFormat:@"%@/%@",model.finish,model.sum];
    self.progressView.progress = model.percent.doubleValue/100.0;
}

@end
